<?php namespace Common\Model;

use Hdphp\Model\Model;

class Category extends Model{
	protected $table = "category";

	protected $validate = array(
		array('cname','required','分类名称不能为空',3,3)
	);

	public function addData($post){
		if($this->create()){
			$this->add($post);
			return true;
		}
		return false;
	}

	public function delData($cid){
		$data = $this->where("cid={$cid}")->find();
		$pid = $data['pid'];
		$this->where("pid={$cid}")->update(array('pid'=>$pid));
		$this->where("cid={$cid}")->delete();
	}

	public function editData(){
		if($this->create()){
			$data = $this->save();
			return true;
		}
		return false;
	}

	public function addSonData(){
		if($this->create()){
			$this->add();
			return true;
		}
		return false;
	}

	public function getNoMy($cid){
		$cids = $this->getSon($this->get(),$cid);
		$cids[] = $cid;
		$strCids = implode(',', $cids);
		return $this->where("cid NOT IN({$strCids})")->get();
	}

	public function getSon($data,$cid){
		$arr = array();
		foreach ($data as $v) {
			if($v['pid'] == $cid){
				$arr[] = $v['cid'];
				$arr = array_merge($arr,$this->getSon($data,$v['cid']));
			}
		}
		return $arr;
	}

}



 ?>